const app = getApp()

Page({
	data: {
		username: '',
		password: '',
		alertMsg: '',
		msgShow: 'hide'
	},

	login: function(e) {
		var ts = this;
		var username = e.detail.value.username;
		var password = e.detail.value.password;
		var api = app.globalData.domain + 'users/login';
		wx.request({
			url: api,
			method: 'POST',
			data: app.getFormData({
				username: username,
				password: password
			}),
			header: app.getHeader(),
			success(res) {
				var t = res.data;
				if (t.code == 1) {
					wx.setStorageSync('userToken', t.result.userToken);
					wx.setStorageSync('userRefreshToken', t.result.refreshToken);
					wx.setStorageSync('effective', t.result.effective);
					var tm = Math.round(new Date().getTime() / 1000, 0);
					wx.setStorageSync('effectiveStart', tm);
					ts.setData({
						alertMsg: '',
						msgShow: 'hide'
					});
					wx.redirectTo({
						url: '../que/que'
					});
				} else {
					ts.setData({
						alertMsg: t.msg,
						msgShow: 'show'
					});
				}
			}
		});
	}
})
